package com.dearbinge.opentapi.content.commodityBeShop;

import com.dearbinge.data.api.commodityBeShop.CommodityBeShopService;
import com.dearbinge.data.pojo.commodityBeShop.CommodityBeShop;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "c：店铺管理商品")
@RestController
@CrossOrigin
@RequestMapping("/commodityBeShop")
@EnableAutoConfiguration
public class CommodityBeShopController extends WebMvcConfigurerAdapter {

    @Autowired
    private CommodityBeShopService commodityBeShopService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody CommodityBeShop commodityBeShop){
        try {
            if (commodityBeShop.getId() != null && commodityBeShop.getId() != 0){
                Integer dataCode = commodityBeShopService.update(commodityBeShop);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = commodityBeShopService.insert(commodityBeShop);
                if (dataCode > 0){
                    return ResultMsg.ok("添加成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据店铺ID与商品ID删除
     */
    @ApiOperation(value = "根据店铺ID与商品ID删除",notes = "根据店铺ID与商品ID删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId",value = "店铺ID",paramType = "query"),
            @ApiImplicitParam(name = "commodityId",value = "商品ID",paramType = "query")})
    @RequestMapping(value = "/deleteByShopId",method = RequestMethod.GET)
    public ResultMsg<String> deleteByShopId(String shopId,String commodityId){
        try {
            Integer dataCode = commodityBeShopService.deleteByShopId(shopId,commodityId);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = commodityBeShopService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 一键开店拷贝商品
     */
    @ApiOperation(value = "一键开店拷贝商品",notes = "一键开店拷贝商品")
    @RequestMapping(value = "/copyCommodity",method = RequestMethod.POST)
    public ResultMsg<String> copyCommodity(@RequestBody CommodityBeShop commodityBeShop){
        try {
            List<CommodityBeShop> list = new ArrayList<>();
            String[] split = commodityBeShop.getCommodityId().split(",");
            for (int index = 0;index < split.length;index++){
                CommodityBeShop commodityBeShop1 = new CommodityBeShop();
                commodityBeShop1.setCommodityId(split[index]);
                commodityBeShop1.setCreateBy(commodityBeShop.getCreateBy());
                commodityBeShop1.setModifyBy(commodityBeShop.getModifyBy());
                commodityBeShop1.setShopId(commodityBeShop.getShopId());
                commodityBeShop1.setCommodityType(1);
                list.add(commodityBeShop1);
            }
            Integer dataCode = commodityBeShopService.insertArr(list);
            if (dataCode > 0){
                return ResultMsg.ok("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
